import json
from datetime import datetime
from urllib.parse import urlencode

from models import TaskVideo, TaskErrorRequest
from pyppeteer_util.video_scraper import VideoScraper
from service.task_error_request_service import TaskErrorRequesService
from service.task_site_service import TaskSiteService
from service.task_video_service import TaskVideoService
from util.md5_util import MD5Util

task_site_service = TaskSiteService()
task_video_service = TaskVideoService()


# 废弃的方法
async def process_site(site, fetch_all_pages=False):
    try:
        param = json.loads(site.param)
        video_list = []
        while True:
            param_url = "?" + urlencode(param)
            url = f"{site.list_link}{param_url}"

            scraper = VideoScraper(url)
            videos = await scraper.start()

            if not videos:
                break

            video_list.extend(build_video_list(videos, site))

            if len(video_list) >= 500 or (not fetch_all_pages and len(video_list) > 0):
                result = task_video_service.create_task_videos(video_list)
                print(f"Processed {len(video_list)} videos. Result: {result}")
                video_list = []

            if not fetch_all_pages:
                break

            # Increment the page number if we're fetching all pages
            if fetch_all_pages:
                page = param.get("page", 1)
                page += 1
                param["page"] = page

    except (ValueError, KeyError, TypeError) as e:
        handle_error(site.id, url, str(e))
    except Exception as e:
        print(f"Unexpected error processing site {site.id}: {e}")

    finally:
        insert_remaining_videos(video_list, site)
        update_site_execute_time(site.id)


def handle_error(site_id, url, error_message):
    print(f"Error processing site {site_id}: {error_message}")
    task_error_request_service = TaskErrorRequesService()
    task_error_request_service.add_request(
        TaskErrorRequest(
            url=url,
            error_message=error_message,
            add_time=datetime.now(),
        )
    )


def insert_remaining_videos(video_list, site):
    if video_list:
        try:
            result = task_video_service.create_task_videos(video_list)
            print(f"Processed {len(video_list)} remaining videos. Result: {result}")
        except Exception as e:
            print(f"Failed to insert remaining videos for site {site.id}: {e}")


def update_site_execute_time(site_id):
    try:
        task_site_service.update_site_execute_time(site_id)
    except Exception as e:
        print(f"Failed to update execute time for site {site_id}: {e}")


def build_video_list(videos, site):
    """构建视频列表"""
    video_list = []
    for video in videos:
        video_name = video.get("video_name")
        image_url = video.get("image_url")
        video_url = video.get("video_url")
        play_time = video.get("play_time")
        type_name = site.type_name
        site_url = site.list_link

        video_new = TaskVideo(
            id=MD5Util.encrypt_string(f"{video_name}{image_url}{video_url}"),
            video_name=video_name,
            image_url=image_url,
            video_url=video_url,
            site_url=site_url,
            play_time=play_time,
            type_name=type_name,
            author="admin",
        )
        video_list.append(video_new)
    return video_list


async def get_remote_site_all():
    site_list = task_site_service.get_all_task_sites("all")
    for site in site_list:
        await process_site(site, fetch_all_pages=True)


async def get_remote_site_new():
    site_list = task_site_service.get_all_task_sites("new")
    for site in site_list:
        await process_site(site, fetch_all_pages=False)
